package cn.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Permission;
import cn.sxt.sys.service.PermissionService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.TreeNode;
import cn.sxt.sys.vo.PermissionVo;

@Controller
@RequestMapping("menu")
public class MenuController {

	@Autowired
	private PermissionService menuService;

	// 跳到menuManager.jsp
	@RequestMapping("toMenuManager")
	public String toMenuManager() {
		return "system/menu/menuManager";
	}

	// menuManager.jsp加载 左边
	@RequestMapping("toMenuLeft")
	public String toMenuLeft() {
		return "system/menu/menuLeft";
	}

	// menuManager.jsp加载 右边
	@RequestMapping("toMenuRight")
	public String toMenuRight() {
		return "system/menu/menuRight";
	}

	// 加载左边 部门树
	@RequestMapping("loadMenuTree")
	@ResponseBody
	public List<TreeNode> loadMenuTree(PermissionVo menuVo) {
		menuVo.setType(SYS_Constast.PERMISSION_TYPE_MENU);
		List<TreeNode> treeNodes = new ArrayList<>();
		List<Permission> menus = menuService.queryPermissionForList(menuVo);
		for (Permission d : menus) {
			Boolean open = d.getOpen() == SYS_Constast.TYPE_PUBLIC_ONE ? true : false;
			Boolean isParent = d.getParent() == SYS_Constast.TYPE_PUBLIC_ONE ? true : false;
			treeNodes.add(new TreeNode(d.getId(), d.getPid(), d.getName(), open, isParent));
		}
		return treeNodes;
	}

	// 菜单数据，分页，模糊
	@RequestMapping("queryAllMenu")
	@ResponseBody
	public DataGridView queryAllMenu(PermissionVo menuVo) {
		menuVo.setType(SYS_Constast.PERMISSION_TYPE_MENU);
		return menuService.queryAllPermission(menuVo);
	}

	// 跳添加页面
	@RequestMapping("toAddMenu")
	public String toAddMenu() {
		return "system/menu/menuAdd";
	}

	// 添加
	@RequestMapping("addMenu")
	@ResponseBody
	public Map<String, Object> addMenu(PermissionVo menuVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			menuVo.setType(SYS_Constast.PERMISSION_TYPE_MENU);
			int i = menuService.addPermission(menuVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateMenu")
	public String toUpdateMenu(PermissionVo menuVo, Model model) {
		Permission menu = menuService.queryPermissionById(menuVo.getId());
		if(menu.getIcon()!=null&&!menu.getIcon().equals("")) {
			menu.setIcon(menu.getIcon().replace("&", "&amp;"));
		}
		model.addAttribute("menu", menu);
		return "system/menu/menuUpdate";
	}

	// 修改
	@RequestMapping("updateMenu")
	@ResponseBody
	public Map<String, Object> updateMenu(PermissionVo menuVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = menuService.updatePermission(menuVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteMenu")
	@ResponseBody
	public Map<String, Object> deleteMenu(PermissionVo menuVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = menuService.deletePermissionById(menuVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除日志
	@RequestMapping("batchDeleteMenu")
	@ResponseBody
	public Map<String, Object> batchDeleteMenu(PermissionVo menuVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = menuVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					menuVo.setId(ids[i]);
					int j = menuService.deletePermissionById(menuVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

}
